import {
	sub
} from '@/common/event.js'
import store from '../index.js'
import moment from 'moment'
import {
	getRqst
} from '../../common/request'

async function delay(time) {
	return new Promise(r => {
		setTimeout(() => {
			r(time)
		}, time * 1000)
	})
}

export default {
	namespaced: true,
	state: {
		list: [{
				name: "消息",
				path: "/pages/index/index",
				icon: "/static/tabicon/index.png",
				icons: "/static/tabicon/indexs.png",
				count: 0,
				selected:true
			},
			{
				name: "通讯录",
				path: "/pages/friend/index",
				icon: "/static/tabicon/friend.png",
				icons: "/static/tabicon/friends.png",
				count: 0,
				selected:false
			},
			{
				name: "会议室",
				path: "/pages/meeting/index",
				icon: "/static/tabicon/meeting.png",
				icons: "/static/tabicon/meetings.png",
				count: 0,
				selected:false
			},
			{
				name: "圈子",
				path: "/pages/news/index",
				icon: "/static/tabicon/news.png",
				icons: "/static/tabicon/newss.png",
				count: 0,
				selected:false
			},
			{
				name: "个人中心",
				path: "/pages/user/index",
				icon: "/static/tabicon/user.png",
				icons: "/static/tabicon/users.png",
				count: 0,
				selected:false
			}
		]
	},
	mutations: {
		setListData: (state, payload) => {
			state.list = payload
		},
		decApplyCount: (state, payload) => {
			if(state.list[1].count > 0){
				state.list[1].count = state.list[1].count - 1
			}
		},
		setCount: (state, payload) => {
			for (let i = 0; i < state.list.length - 1; i++) {
				if (state.list[i].name == payload.key || i == payload.key) {
					state.list[i].count = payload.data
					break
				}
			}
		},
		setCurrent: (state, payload) => {
			for(let o of state.list){
				if(o.path.includes(payload) || payload.includes(o.path)){
					o.selected = true
				}else{
					o.selected = false
				}
			}
		}
	},
	actions: {
		async init({
			commit,
			state
		}, {
			payload
		}) {
			await delay(3)
			const sysinfo = store.state.common.systemInfo
			if (sysinfo.customtabbar == "custom") {
				const ret = await getRqst().post(`common/common/getdata/tabbar`, {})
				if (ret && ret.success && ret.data && ret.data.length > 0) {
					commit(`tabbar/setListData`, ret.data)
				}
			}
			
			
		},
		async getapplybadge({
			commit,
			state
		}, {
			payload
		}) {
			// 获取申请徽标
			const user = uni.getStorageSync("user")
			const ret = await getRqst().post(`bgyx/bgyx/getdata/userapply`,{where:{toid:user.id,status:"申请中"}})
			if(ret && ret.success){
				commit(`setCount`,{
					key:"通讯录",
					data:ret.data.length
				})
			}
		}
	},
	init: async function() {
		console.log("common初始化")
		store.dispatch({
			type: "tabbar/init",
			payload: {}
		})
		
		const user = uni.getStorageSync("user")
		if(user && user.id){
			store.dispatch({
				type: "tabbar/getapplybadge",
				payload: {}
			})
		}
		
		sub("loginSuccess",()=>{
			store.dispatch({
				type: "tabbar/getapplybadge",
				payload: {}
			})
		})
		
		sub("socket_touser_friendapply",()=>{
			store.dispatch({
				type: "tabbar/getapplybadge",
				payload: {}
			})
		})
	}
}